package com.yanfan.parking;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yanfan.common.core.controller.BaseController;
import com.yanfan.common.core.domain.AjaxResult;
import com.yanfan.common.utils.DateUtils;
import com.yanfan.parking.domain.*;
import com.yanfan.parking.mapper.IbmsParkingPlaceMapper;
import com.yanfan.parking.service.INjInParkingRecordService;
import com.yanfan.parking.service.INjOutParkingRecordService;
import com.yanfan.parking.service.INjParkingCarInfoService;
import com.yanfan.parking.service.INjParkingHourFlowService;
import com.yanfan.parking.utils.AESUtil;
import com.yanfan.parking.vo.InOutRecordVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLDecoder;
import java.util.*;
import org.apache.commons.codec.binary.Base64;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 停车场对接api
 */

@Api(tags = "停车场系统对接api")
@RestController
@Slf4j
@RequestMapping("/api")
public class ApiParkingController extends BaseController {

    @Value("${api.dahua.url}")
    private String url;

    @Resource
    private IbmsParkingPlaceMapper ibmsParkingPlaceMapper;

    @Resource
    private INjInParkingRecordService iNjInParkingRecordService;

    @Resource
    private INjOutParkingRecordService outParkingRecordService;
    @Resource
    private INjParkingHourFlowService parkingHourFlowService;

    @Resource
    private INjParkingCarInfoService carInfoService;


    /**
     * 上传进出记录接口
     */
    @ApiOperation("上传车辆进出记录")
    @PostMapping("/inRecord")
    public void  inRecord(@RequestBody String param, HttpServletRequest request, HttpServletResponse response) throws Exception {
            System.out.println("+++++++++++++++++++接收到车辆入场推送接口数据+++++++++++++++++++++");
            System.out.println(param);
            param = URLDecoder.decode(param, "utf-8");
            param = param.replace(" ", "+");
            System.out.println(param);
            String userAgent = request.getHeader("User-Agent");
            userAgent = userAgent.substring(userAgent.lastIndexOf("_") + 1, userAgent.length());
            String[] nums = userAgent.split("[\\D]+");
            int ciphertnum=0;
            String lastNum = nums[nums.length - 1];
            if(lastNum.length()>1){
                System.out.println(lastNum.length());
                lastNum = lastNum.substring(lastNum.length() - 1, lastNum.length());
            }
            ciphertnum = Integer.parseInt(lastNum);
            String key = userAgent.substring(ciphertnum, ciphertnum + 16);
            System.out.println(key);
            String s1 = AESUtil.aesDecrypt(param, key);
            System.out.println(s1);
            //解密
            byte[] raw = key.getBytes("utf-8");
            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE,skeySpec);
            byte[] decode = new Base64().decode(param);
            byte[] bytes = cipher.doFinal(decode);
            String s = new String(bytes, "utf-8");
            System.out.println(s);
            //JSONObject jsonObject = JSONObject.parseObject(s);
        NjInParkingRecord njInParkingRecord = JSONObject.parseObject(s, NjInParkingRecord.class);
        iNjInParkingRecordService.save(njInParkingRecord);
    }

    @ApiOperation("上传车辆进出记录")
    @PostMapping("/outRecord")
    public void  outRecord(@RequestBody String param, HttpServletRequest request, HttpServletResponse response) throws Exception {
        System.out.println("+++++++++++++++++++接收到车辆出场推送接口数据+++++++++++++++++++++");
        System.out.println(param);
        param = URLDecoder.decode(param, "utf-8");
        param = param.replace(" ", "+");
        System.out.println(param);
        String userAgent = request.getHeader("User-Agent");
        userAgent = userAgent.substring(userAgent.lastIndexOf("_") + 1, userAgent.length());
        String[] nums = userAgent.split("[\\D]+");
        int ciphertnum=0;
        String lastNum = nums[nums.length - 1];
        if(lastNum.length()>1){
            System.out.println(lastNum.length());
            lastNum = lastNum.substring(lastNum.length() - 1, lastNum.length());
        }
        ciphertnum = Integer.parseInt(lastNum);
        String key = userAgent.substring(ciphertnum, ciphertnum + 16);
        System.out.println(key);
        String s1 = AESUtil.aesDecrypt(param, key);
        System.out.println(s1);
        //解密
        byte[] raw = key.getBytes("utf-8");
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE,skeySpec);
        byte[] decode = new Base64().decode(param);
        byte[] bytes = cipher.doFinal(decode);
        String s = new String(bytes, "utf-8");
        System.out.println(s);
        //JSONObject jsonObject = JSONObject.parseObject(s);
        NjOutParkingRecord njOutParkingRecord = JSONObject.parseObject(s, NjOutParkingRecord.class);
        outParkingRecordService.save(njOutParkingRecord);
    }

    /**
     * 车流量统计
     * @return
     */
    @ApiOperation("24小时车流量")
    @GetMapping("/hourFlow")
    public AjaxResult hourFlow(){
        //当天日期
        String day = DateUtils.parseDateToStr("yyyy-MM-dd", new Date());
        List<NjParkingHourFlow> list = parkingHourFlowService.list(new LambdaQueryWrapper<NjParkingHourFlow>()
                .like(NjParkingHourFlow::getUpdateDate, day)
                .orderByAsc(NjParkingHourFlow::getDates));
        return success(list);
    }



    /**
     * 趋势统计图
     *
     * @return
     */
    /*@ApiOperation("趋势统计图")
    @GetMapping("/chart")
    public JSONObject chart() {
        JSONObject jsonObject = new JSONObject();
        String time = DateUtils.getDate();
        //小时进场数据
        List<InOutRecordVo> inList = inOutRecordMapper.selectByChartIn(time);
        //小时出场数据
        List<InOutRecordVo> outList = inOutRecordMapper.selectByChartOut(time);

        jsonObject.put("inList", inList);
        jsonObject.put("outList", outList);

        return jsonObject;
    }*/
}
